package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service  //注意: 是dubbo里面的服务标签
//@Transactional  项目4再讲
public class UserInfoServiceImpl implements IUserInfoService {


    //trip-member-server  调用 trip-member-api 接口
    //trip-member-server  调用 trip-member-sever 接口

    @Autowired  //组件与组件间调用
    private UserInfoMapper userInfoMapper;
    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public UserInfo get(long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    public boolean checkPhone(String phone) {
        return userInfoMapper.selectByPhone(phone) > 0;
    }

    @Override
    public void sendVerifyCode(String phone) {
        //创建手机验证码
        String code = UUID.randomUUID().toString().replace("-", "").substring(0, 4);

        //缓存到redis中 TODO
        userInfoRedisService.setVerfiyCode(phone, code);


        //发送手机验证码
        StringBuilder sb = new StringBuilder(100);
        sb.append("你的验证码是:").append(code).append(", 请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");

        //假装已经发送了
        System.out.println(sb.toString());
    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) throws LogicException {

        //校验参数是否为空
        AssertUtil.hasLength(phone, "手机号码不能为空");
        AssertUtil.hasLength(nickname, "昵称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "确定密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");

        //2次输入密码是否一致
        if (!password.equals(rpassword)) {
            throw new LogicException("两次输入密码不一致");
        }
        //判断手机号码格式是否正确
        boolean flag = false;
        Pattern p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$");
        Matcher m = p.matcher(password);
        flag = m.matches();
        if(flag){
            throw new LogicException("手机号码格式错误");
        }


        //必须校验手机号码是否存在
        if (userInfoMapper.selectByPhone(phone) > 0) {
            throw new LogicException("手机号已被注册");
        }
        //必须校验短信验证码是否正确
        String code = userInfoRedisService.getVerfiyCode(phone);
        //当从redis里拿到的code为空,即时效性已过,或者不等于请求里的参数时,抛出异常
        if (code == null || !code.equals(verifyCode) ) {
            throw new LogicException("验证码不正确");
        }

        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setNickname(nickname);
        userInfo.setPassword(password);
        userInfo.setHeadImgUrl("/images/default.jpg");
        //设置默认等级1,.状态正常
        userInfo.setLevel(1);
        userInfo.setState(UserInfo.STATE_NORMAL);
        userInfoMapper.insert(userInfo);
    }

    public UserInfo selectByPhoneAndPassword(String username, String password) {
        return userInfoMapper.selectByPhoneAndPassword(username,password);
    }

    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }

}
